package com.masterstroke.template.controller;

import com.masterstroke.template.common.result.Result;
import com.masterstroke.template.common.utils.Encipher;
import com.masterstroke.template.pojo.domain.Map;
import com.masterstroke.template.pojo.vo.MapVO;
import com.masterstroke.template.service.MapService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("map")
@RequestMapping("map")
@Api("轮播图")
@Slf4j
public class MapController {

    @Autowired
    private MapService mapService;

    @ApiOperation("获取轮播图")
    @GetMapping("getAllMap")
    public Result<MapVO> getMap(){
        List<Map> list = mapService.list();
        MapVO mapVO = new MapVO();
        log.info("list:{}",list);
        for (Map map : list) {
            //设置轮播图对应的商品id
            mapVO.setMapImg(map.getMapImg());
            mapVO.setProductId(map.getProductId());
        }
        return Result.success(mapVO);
    }

    @ApiOperation("添加轮播图")
    @PutMapping("addMap")
    public Result addMap(String mapImg,String productId){
        Map map = new Map();
        String mapId = Encipher.getUUID();
        map.setMapId(mapId);
        map.setMapImg(mapImg);
        map.setProductId(productId);
        mapService.save(map);
        return Result.success();
    }
}
